package com.qding.park.adapter.ark.adapter.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import com.qding.park.adapter.ark.adapter.enums.CsvDelEnum;

/**
 * @ClassName OwnerInfoRecord
 * @Description TODO(csv 用户信息记录)
 * @author LeiYang
 * @Date 2017年5月17日 下午2:21:13
 * @version 1.0.0
 */
public class FtpCxvAnalysis {

    private Map<String, Integer> headers = new HashMap<String, Integer>();

    public void headeAnalysis(String[] row) {

        headers.clear();
        for (int i = 0; i < row.length; i++) {
            headers.put(row[i], i);
        }
    }

    public Map<String, String> bodyAnalysis(String[] row) {

        Map<String, String> rowMap = new HashMap<String, String>();

        for (Map.Entry<String, Integer> entry : headers.entrySet()) {
            rowMap.put(entry.getKey(), row[entry.getValue()]);
        }
        return rowMap;
    }
    
    public  boolean isDel(String[] row){
        String del=null;
        if(this.headers.containsKey("is_del")){
            del=row[headers.get("is_del")];
        }else if(this.headers.containsKey("IS_DEL")){
            del=row[headers.get("IS_DEL")];
        }else if(this.headers.containsKey("del_flag")){
            del=row[headers.get("del_flag")];
        } else{
            throw new RuntimeException("没有找到删除标识");
        }
        
        if(CsvDelEnum.DEL.getValue().equals(del)){
            return true;
        }else {
            return false;
        }
    }
    
    public   <T> T bodyAnalysis(String[] row,Class<T> objClass) throws InstantiationException, IllegalAccessException {
        
        T  objBean=objClass.newInstance();
        
        Field[]  fields= objClass.getDeclaredFields();
        
        for(Field  field:fields){
            String fieldName=field.getName();
            field.setAccessible(true);
            field.set(objBean, row[headers.get(fieldName)]);
        }
        
        return objBean;
    }

}
